package com.example.springevent.demos;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Event02AnnotationListenerDemo {

	private static final Logger log = LoggerFactory.getLogger(Event02AnnotationListenerDemo.class);

	public static void main(String[] args) {
		// 创建注解驱动 Spring 应用上下文
		// 包扫描:
		// @link Event02AnnotationListenerDemo
		// @link DemoEventMethodAnnotationListener
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.example.springevent.demos");

		// 发布 Spring 事件
		log.info("开始发布事件DemoEvent");
		context.publishEvent(new DemoEvent("event-hello world"));

		
		log.info("开始发布事件String");
		// 不推荐使用PayloadApplicationEvent
		context.publishEvent(new PayloadApplicationEvent<String>(context, "event-hello world"));

		// 关闭 Spring 应用上下文
		context.close();
	}

}
